Imports System.Xml
Namespace Data

  ''' <summary>
  ''' List of US States
  ''' </summary>
  ''' <remarks></remarks>
  Public Class UsStateCollection
    Inherits System.Collections.ObjectModel.ReadOnlyCollection(Of State)

    ''' <summary>
    ''' Converts a state name to state code or state code to state name.
    ''' </summary>
    ''' <param name="nameOrCode">State name or code.</param>
    ''' <returns>State name or code.</returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertNameOrCode(ByVal nameOrCode As String) As String

      If String.IsNullOrEmpty(nameOrCode) Then
        Throw New ArgumentNullException("nameOrCode")
      End If

      Dim stateResult As String = String.Empty

      'Get state info
      Dim stateData As New XmlDocument
      stateData.LoadXml(My.Resources.StatesXML)

      Dim stateInfo As XmlNode = stateData.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, "//state[@{0} = '{1}']", IIf(nameOrCode.Length = 2, "abbreviation", "name"), nameOrCode.ToUpper(CultureInfo.InvariantCulture)))

      If Not IsNothing(stateInfo) AndAlso stateInfo.OuterXml.Length > 0 Then
        If nameOrCode.Length = 2 Then

          stateResult = stateInfo.Attributes("name").InnerText

        Else

          stateResult = stateInfo.Attributes("abbreviation").InnerText

        End If

      End If

      Return stateResult

    End Function

    ''' <summary>
    ''' Constructor that auto loads the states list.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
      MyBase.New(DataLists.StatesList)
    End Sub

    ''' <summary>
    ''' Constructor that loads the states
    ''' </summary>
    ''' <param name="list">List of <see cref="State">states</see>.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal list As System.Collections.Generic.IList(Of State))
      MyBase.New(list)
    End Sub

  End Class
End Namespace
